package com.tianxiabuyi.persist;


import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;

/**
 * 工具类<br>
 * 2015-05-07
 */
public final class PersistTools {
    private PersistTools() {
    }

    public static String getDesc(String value) {
        return getDesc(value, "暂无");
    }

    public static String getDesc(String value, String defaultShow) {
        if ((value + "").equals("null")) {
            return defaultShow;
        }
        return value.trim();
    }

    public static Integer getInt(String value, Integer defaultInteger) {
        Integer v = getInt(value);
        if (v == null) {
            return defaultInteger;
        }
        return v;
    }

    public static Integer getInt(String value) {
        try {
            return Integer.parseInt(value.trim());
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 修改对象内容
     * @param oldItem
     * @param newItem
     */
    public static void modItem(Object oldItem, Object newItem) {
        Object[] getters = DataSource.getterMaps
                .get(oldItem.getClass().getName()).values().toArray();
        HashMap<String, Method> setters = DataSource.setterMaps.get(oldItem
                .getClass().getName());
        for (int i = 0; i < getters.length; i++) {
            Method getter = (Method) getters[i];
            int count = getter.getParameterCount();
            String allName = getter.toString();
            try {
                Object value = getter.invoke(newItem, new Object[count]);
                if (value != null) {
                    String gName = getter.getName();
                    String sName = "s" + gName.substring(1, gName.length());
                    Method setter = setters.get(sName);
                    if (setter != null) {
                        setter.invoke(oldItem, new Object[] { value });
                    }
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static Class<?>[] getClasses(String packageName)
            throws ClassNotFoundException, IOException {
        ClassLoader classLoader = Thread.currentThread()
                .getContextClassLoader();
        assert classLoader != null;
        String path = packageName.replace('.', '/');
        Enumeration<URL> resources = classLoader.getResources(path);
        List<File> dirs = new ArrayList<File>();
        while (resources.hasMoreElements()) {
            URL resource = resources.nextElement();
            dirs.add(new File(resource.getFile()));
        }
        ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
        for (File directory : dirs) {
            classes.addAll(findClasses(directory, packageName));
        }
        return classes.toArray(new Class[classes.size()]);
    }

    public static List<Class<?>> findClasses(File directory, String packageName)
            throws ClassNotFoundException {
        List<Class<?>> classes = new ArrayList<Class<?>>();
        if (!directory.exists()) {
            return classes;
        }
        File[] files = directory.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                assert !file.getName().contains(".");
                classes.addAll(findClasses(file,
                        packageName + "." + file.getName()));
            } else if (file.getName().endsWith(".class")) {
                classes.add(Class.forName(packageName
                        + '.'
                        + file.getName().substring(0,
                        file.getName().length() - 6)));
            }
        }
        return classes;
    }
}
